from selenium import webdriver
from selenium.webdriver.common.by import By
import time

# 打开浏览器访问网址
driver = webdriver.Chrome()
driver.get('https://mail.163.com')

# 添加登录时的cookie
cookies = [
    {'name': 'cm_last_info',
     'value': 'dT13dTk0Mjk4MTk1NCU0MDE2My5jb20mZD1odHRwcyUzQSUyRiUyRm1haWwuMTYzLmNvbSUyRmpzNiUyRm1haW4uanNwJTNGc2lkJTNEQ0JPdnV6cE5taGpBeHNBY0FiTk5YS29abE1TenZCVHImcz1DQk92dXpwTm1oakF4c0FjQWJOTlhLb1psTVN6dkJUciZoPWh0dHBzJTNBJTJGJTJGbWFpbC4xNjMuY29tJTJGanM2JTJGbWFpbi5qc3AlM0ZzaWQlM0RDQk92dXpwTm1oakF4c0FjQWJOTlhLb1psTVN6dkJUciZ3PWh0dHBzJTNBJTJGJTJGbWFpbC4xNjMuY29tJmw9LTEmdD0tMSZhcz10cnVl'},
    {'name': 'Coremail', 'value': '5da9aae05d564%CBOvuzpNmhjAxsAcAbNNXKoZlMSzvBTr%g6a51.mail.163.com'},
    {'name': 'MAIL_ENTRY_CS', 'value': '1a9cdb3df481895ad988ff08c761ed33'},
    {'name': 'MAIL_ENTRY_INFO',
     'value': '1|0|mail163|mail163_letter|221.234.129.46|d1eb9dc2122ee7092bac699c3ffb507e_v1|'},
    {'name': 'NTES_SESS',
     'value': '1xYj05Ce2Qffu.DZM8.EHc9tM9XtAqP8N5jGdM7UWQeFuWdmu57zsx4WuRJBq6MjX1Y2vUd9ueqek0h_keeL._1xcuuVXjrL5oAdk5VwZZFxLtE62WJJ9D9g831qgvonNrFnBajkuS_fSQE8yn7of7V2uA9Sv_Okn98cyuf4BWjaWfcheeAkYhSxx8exgR55BxaN5E5fqShqx'}
]
for cookie in cookies:
    driver.add_cookie(cookie)

# 刷洗页面
driver.refresh()

# 点击写信
driver.find_element(By.ID, '_mail_component_149_149').click()
# 定位收件人和主题
driver.find_element(By.CLASS_NAME, 'nui-editableAddr-ipt').send_keys('123@163.com')
time.sleep(2)
driver.find_element(By.XPATH, '//header/div[2]/div[1]/div/div/input').send_keys('123')

# 进入内容frame
time.sleep(2)
iframe = driver.find_element(By.CLASS_NAME, 'APP-editor-iframe')
driver.switch_to.frame(iframe)
time.sleep(2)
driver.find_element(By.CLASS_NAME, 'nui-scroll').send_keys('123')

# 返回主页面面发送
time.sleep(2)
driver.switch_to.default_content()
# 点击发送按钮
time.sleep(2)
driver.find_elements(By.CLASS_NAME, 'nui-btn-text')[8].click()

# 退出浏览器
time.sleep(2)
driver.quit()
